package com.yb.nowcoder;

import java.util.ArrayList;
import java.util.List;

public class Mywork {

    public static void main(String[] args) {
        System.out.println(josephus(50, 3));
        String[] a = new String[]{"a", "a", "b", "b", "c", "a", "c", "d"};
        String[] arr = charCount(a);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        System.out.println(test());
        System.out.println(getValue(2));

    }

    /**
     * 约瑟夫环问题
     *
     * @param n
     * @param m
     * @return
     */
    public static int josephus(int n, int m) {
        List<Integer> in = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            in.add(i);
        }
        List<Integer> out = new ArrayList<>();
        //计数开始
        int k = 0;
        while (in.size() > 0) {
            k = k + m;
            k = k % (in.size()) - 1;
            //到达末尾
            if (k < 0) {
                out.add(in.get(in.size() - 1));
                in.remove(in.size() - 1);
                k = 0;
            } else {
                out.add(in.get(k));
                in.remove(k);
            }
        }
        return out.get(out.size() - 1);
    }

    /**
     * 统计数组中连续字母的个数
     *
     * @param arr
     * @return
     */
    public static String[] charCount(String[] arr) {
        int len = arr.length;
        List<String> list = new ArrayList<>();
        int i = 0;
        while (i <= len - 1) {
            int count = 1;
            list.add(arr[i]);
            if (i + 1 <= len - 1) {
                while (arr[i].equals(arr[i + 1])) {
                    count++;
                    i++;
                }
            }
            list.add(String.valueOf(count));
            i++;
        }
        return list.toArray(new String[list.size()]);
    }

    /**
     * 3个空瓶子换一瓶汽水
     * 给定n个空瓶子
     *
     * @param n
     */
    public static int bottle(int n) {
        //直接用数学方法
        if (n == 1) {
            return 0;
        } else if (n == 2) {
            return 1;
        } else {
            return n / 3 + bottle(n / 3 + n % 3);
        }
    }

    /**
     * 返回几？
     *
     * @return
     */
    public static int test() {
        try {
            return 1;
        } catch (Exception e) {
            return 2;
        } finally {
            return 3;
        }
    }

//    public static int test1() {
//        String s;
//        System.out.println("s="+s);
//    }

    /**
     * switch没有break
     * @param i
     * @return
     */
    public static int getValue(int i) {
        int result = 0;
        switch (i) {
            case 1:
                result = result + i;
            case 2:
                result = result + 2 * i;
            case 3:
                result = result + 3 * i;
        }
        return result;
    }
}
